package com.my.zidingyikongjian.view.four;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;

/**
 * 项目名称：Canvas 变换
 * 创建人：mwb
 * 创建时间：2018/7/16 15:41
 */

public class MyCanvasTransformation extends View {
    private int flag;

    public MyCanvasTransformation(Context context, int flag) {
        super(context);
        this.flag = flag;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setStrokeWidth(5);
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);

        if (flag == 1) { // 平移
            RectF rectF = new RectF(0, 0, 400, 220);
            canvas.drawRect(rectF, paint);

            paint.setColor(Color.RED);
            canvas.translate(100, 100); // 平移
            canvas.drawRect(rectF, paint);
        } else if (flag == 2) { // 旋转
            RectF rectF = new RectF(300, 10, 500, 100);
            canvas.drawRect(rectF, paint);

            paint.setColor(Color.RED);
            canvas.rotate(30); // 旋转
            canvas.drawRect(rectF, paint);
        } else if (flag == 3) { // 缩放
            RectF rectF = new RectF(10, 10, 200, 100);
            canvas.drawRect(rectF, paint);

            paint.setColor(Color.RED);
            canvas.scale(0.5f, 1); // 缩放
            canvas.drawRect(rectF, paint);
        } else if (flag == 4) { // 扭曲
            RectF rectF = new RectF(10, 10, 200, 100);
            canvas.drawRect(rectF, paint);

            paint.setColor(Color.RED);
            canvas.skew(1.732f, 0);// 扭曲
            canvas.drawRect(rectF, paint);
        } else if (flag == 5) { // 裁剪
            canvas.drawColor(Color.RED);
            RectF rectF = new RectF(100, 100, 200, 200);
            canvas.drawRect(rectF, paint);
            canvas.clipRect(rectF);
            canvas.drawColor(Color.GREEN);
        } else if (flag == 6) { // 保存与恢复
            canvas.drawColor(Color.RED);
            // 保存画布大小为全屏大小
            canvas.save();

            canvas.clipRect(new RectF(100, 100, 800, 800));
            canvas.drawColor(Color.GREEN);
            // 保存画布大小位Rect(100,100,800,800)
            canvas.save();

            canvas.clipRect(new RectF(200, 200, 700, 700));
            canvas.drawColor(Color.BLUE);
            // 保存画布大小位RectF(200, 200, 700, 700)
            canvas.save();

            canvas.clipRect(new RectF(300, 300, 600, 600));
            canvas.drawColor(Color.BLACK);
            // 保存画布大小位RectF(300, 300, 600, 600)
            canvas.save();

            canvas.clipRect(new RectF(400, 400, 500, 500));
            canvas.drawColor(Color.WHITE);
            // 保存画布大小位RectF(400, 400, 500, 500)
            canvas.save();

        } else if (flag == 7) { // 保存与恢复
            canvas.drawColor(Color.RED);
            // 保存画布大小为全屏大小
            canvas.save();

            canvas.clipRect(new RectF(100, 100, 800, 800));
            canvas.drawColor(Color.GREEN);
            // 保存画布大小位Rect(100,100,800,800)
            canvas.save();

            canvas.clipRect(new RectF(200, 200, 700, 700));
            canvas.drawColor(Color.BLUE);
            // 保存画布大小位RectF(200, 200, 700, 700)
            canvas.save();

            canvas.clipRect(new RectF(300, 300, 600, 600));
            canvas.drawColor(Color.BLACK);
            // 保存画布大小位RectF(300, 300, 600, 600)
            canvas.save();

            canvas.clipRect(new RectF(400, 400, 500, 500));
            canvas.drawColor(Color.WHITE);
            // 保存画布大小位RectF(400, 400, 500, 500)
            canvas.save();

            canvas.restore();
            canvas.restore();
            canvas.restore();
            canvas.drawColor(Color.YELLOW);
        }
    }
}
